home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
oper_sys
/
weyl
/
weyl!!.lha
/
error.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-08-15
|
2KB
|
105 lines
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
// Error function. fmt is a printf-style format string.
// The formatted message is written to the error
// stream and then the function calls exit(1).
#include <stream.h>
#include <stdarg.h>
#include <stdlib.h>
#include "domains.h"
void handleFormatOption (const char *fmt, va_list ap) {
char ch;
switch (ch = *fmt++) {
// %% becomes a single %
case '%':
cerr.put('%');
break;
// output string
case 's':
{
char *s = va_arg(ap, char*);
cerr << s;
}
break;
// output decimal integer
case 'd':
{
int s = va_arg(ap, int);
cerr << s;
}
break;
// output character
case 'c':
{
int s = va_arg(ap, int);
cerr.put(s);
}
break;
// Domain Element
case 'E':
{
DomainElt *d = va_arg(ap, DomainElt *);
if (!d) cerr << "[Null pointer]";
else d->printon(cerr);
}
break;
default:
cerr << "\nunknown % sequence: %" << char(ch) << "\n";
break;
}
}
void error(const char *fmt ...)
{
va_list ap;
va_start(ap, fmt);
char ch;
cerr << "Error: ";
// loop across format string
while (ch = *fmt++)
// output normal chars
if (ch != '%')
cerr.put(ch);
else
// found % sequence
handleFormatOption(fmt, ap);
// all done
va_end(ap);
exit(1);
}
void warn(const char *fmt ...)
{
va_list ap;
va_start(ap, fmt);
char ch;
cerr << "Warning: ";
// loop across format string
while (ch = *fmt++)
// output normal chars
if (ch != '%')
cerr.put(ch);
else
// found % sequence
handleFormatOption(fmt, ap);
// all done
va_end(ap);
cerr.put('\n');
}